package com.lxm.shownews;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.lxm.shownews.bean.ChannelInfo;
import com.lxm.shownews.channel.ChannelJsonUtils;
import com.lxm.shownews.channel.DrawerActivity;
import com.lxm.shownews.commons.DBUtils;
import com.lxm.shownews.commons.Urls;
import com.lxm.shownews.utils.SPUtils;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.lxm.shownews.utils.SPUtils.SHARED_LAUNCHNUM;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String picUrl = "";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Picasso.with(MainActivity.this).load((String) msg.obj).into(welcomeImg);
            }
        }
    };
    private ImageView welcomeImg;
    List<ChannelInfo> channels;

    private void initChannel() {
//        OkHttpClient okHttp = new OkHttpClient();
        Request request = new Request.Builder().
                url(Urls.URL_CHANNEL_NEWS).addHeader("apikey", Urls.BAIDU_API_KEY).build();
        okHttp.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.i("onfailure", request.toString() + "\t" + e.toString());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    String channelStr = response.body().string();
                    Log.i("onResponse--", channelStr);
                    channels = ChannelJsonUtils.readChannelBeans(channelStr);
                    initChannelInfoList(channels);
//                    for (ChannelInfo channelInfo : channels) {
////                        SPUtils.put(MainActivity.this, channelInfo.getChannelId(), channelInfo.getName());
//                        boolean b = DBUtils.getDbInstance(MainActivity.this).insertChannelInfo(channelInfo);
//                        Log.i("--b--", b + "");
//                    }
                    DBUtils.getDbInstance(MainActivity.this).insertChannelInfoList(channels);
                    Log.i("channel", channels.toString());
                }
            }
        });
    }

    OkHttpClient okHttp;
    private static int launchNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        /*set it to be full screen*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        welcomeImg = (ImageView) findViewById(R.id.welcomeImg);
        Button skipBtn = (Button) findViewById(R.id.skip);
        skipBtn.setOnClickListener(this);
        launchNum = (Integer) SPUtils.get(this, SHARED_LAUNCHNUM, "launchNum", 0);
        okHttp = new OkHttpClient();
        Request request = new Request.Builder().url(Urls.TXMEINV + "?num=1").addHeader("apikey", Urls.BAIDU_API_KEY).build();
        okHttp.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                //NOT UI Thread
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    System.out.println(response.code());
                    System.out.println(result);
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                        String pic = jsonObject.getJSONArray("newslist").getJSONObject(0).getString("picUrl");
//                        String pic = jsonObject.getJSONObject("0").getString("picUrl");
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = pic;
                        handler.sendMessage(msg);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        initChannel();
    }

    private void initChannelInfoList(List<ChannelInfo> channelInfoList) {
        Map<String, Integer> channelSortMap = new HashMap<>();
        channelSortMap.put("5572a108b3cdc86cf39001cd", 1);  //5572a108b3cdc86cf39001cd  国内焦点
        channelSortMap.put("5572a108b3cdc86cf39001ce", 2);  //5572a108b3cdc86cf39001ce 国际焦点
        channelSortMap.put("5572a108b3cdc86cf39001cf", 3);  //5572a108b3cdc86cf39001cf 军事焦点
        channelSortMap.put("5572a108b3cdc86cf39001d0", 4);  //5572a108b3cdc86cf39001d0 财经焦点
        channelSortMap.put("5572a108b3cdc86cf39001d1", 5);  //5572a108b3cdc86cf39001d1 互联网焦点
        channelSortMap.put("5572a108b3cdc86cf39001d2", 6);  //5572a108b3cdc86cf39001d2 房产焦点
        channelSortMap.put("5572a108b3cdc86cf39001d3", 7);  //5572a108b3cdc86cf39001d3  汽车焦点
        channelSortMap.put("5572a108b3cdc86cf39001d4", 8);  //"5572a108b3cdc86cf39001d4""体育焦点"
        channelSortMap.put("5572a108b3cdc86cf39001d5", 9);  //5572a108b3cdc86cf39001d5  娱乐
        channelSortMap.put("5572a108b3cdc86cf39001d6", 10);  //5572a108b3cdc86cf39001d6  游戏
        channelSortMap.put("5572a108b3cdc86cf39001d7", 11);  //5572a108b3cdc86cf39001d7  教育
        channelSortMap.put("5572a108b3cdc86cf39001d8", 12);  //"5572a108b3cdc86cf39001d8""女人焦点"
        channelSortMap.put("5572a108b3cdc86cf39001d9", 13);  //"5572a108b3cdc86cf39001d9""科技焦点"
        channelSortMap.put("5572a109b3cdc86cf39001da", 14);  //"5572a109b3cdc86cf39001da" "社会焦点"
        for (ChannelInfo channelInfo : channelInfoList) {
            for (Iterator<String> iter = channelSortMap.keySet().iterator(); iter.hasNext(); ) {
                String next = iter.next();
                if (next.equals(channelInfo.getChannelId())) {
                    channelInfo.setSortNum(channelSortMap.get(next));
                }

            }
        }
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, DrawerActivity.class);
//        intent.putExtras()
        startActivity(intent);
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SPUtils.put(this, SHARED_LAUNCHNUM, "launchNum", ++launchNum);
        Log.i("----", SPUtils.get(this, SHARED_LAUNCHNUM, "launchNum", 0) + "");
    }
}
